﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class GameManager : MonoSingleton<GameManager>
{
    //游戏角色
    public GameObject mainPlayer;
    //游戏开始
    public bool isGameStart;
    //是否处于游戏控制状态
    public bool isGameControl;
    //是否处于暂停状态
    public bool isPause;
    //游戏是否结束
    public bool isGameOver;


   
    public void Start()
    {
        if (DataManager.Instance.mUserData.isFirstGame)
        {
            DataManager.Instance.mUserData.isFirstGame = false;
            DataManager.Instance.ResetUserData();
        }
        DataManager.Instance.LoadData();        
        UILoadOpen();
        StartCoroutine(LoadScene());      
    }
    private IEnumerator LoadScene(string nextSceneName = "Main")
    {
        AsyncOperation op = SceneManager.LoadSceneAsync(nextSceneName);
        op.allowSceneActivation = false;
        float prograss = 0;
        while(prograss < 0.75f)
        {
            if (prograss <= op.progress + 0.1f)
            {
                prograss += 0.01f;                
            }
            EventCenter.Broadcast(CL.EventType.SetPrograssBar, prograss);
            yield return new WaitForEndOfFrame();
        }
        //!DataCfgManager.Instance.bLoad || prograss < 1f（有配置文件如二进制表等需要多加个判定）
        while (prograss < 1f)
        {
            if (prograss <= op.progress + 0.1f)
            {
                prograss += 0.01f;
            }
            EventCenter.Broadcast(CL.EventType.SetPrograssBar, prograss);
            yield return new WaitForEndOfFrame();
        }       
        op.allowSceneActivation = true;
        //加载玩家角色在UIMain场景进行
        mainPlayer = GameObject.Find("Player");
        //UIMainOpen();  跳转场景无法实现事件监听广播
        yield return 0;
    }

    #region 游戏主框架！！！（UI界面打开与关闭，游戏BGM，游戏数据的改变mUserData、mGameData，）  PS：游戏状态不需要此框架里更改
    public void UILoadOpen()
    {
        
    }    
   

    public void UIMainOpen()
    {   
        //******没用UI框架基本就会是这个熊样儿******
        EventCenter.Broadcast(CL.EventType.ShowUIMain);
        UIGameClose();
        UIGameOverClose();
        UIResetUserDataClose();
        UIShopClose();        
        UIRankClose();
    }
    public void UIMainClose()
    {
        EventCenter.Broadcast(CL.EventType.HideUIMain);
    }

    public void UIGameOpen()
    {
        UIMainClose();
        UIGameOverClose();
        EventCenter.Broadcast(CL.EventType.ShowUIGame);            
        SpawnManager.Instance.InitSpawnProperty();
        DataManager.Instance.ResetGameData();
    }
    public void UIGameClose()
    {
        EventCenter.Broadcast(CL.EventType.HideUIGame);  
    }


    public void UIGameOverOpen()
    {
        //不能立即显示UIGameOver
        StartCoroutine(Delay());        
    }
    /// <summary>
    /// 注意：协程下面有代码才能延迟发挥作用
    /// </summary>
    /// <returns></returns>
    private IEnumerator Delay()
    {
        yield return new WaitForSeconds(2);
        UIGameClose();
        //把所有平台放回对象池            
        //for (int i = 0; i < GameObject.Find("Pool/work").GetComponentsInChildren<Transform>().Length; i++)
        //{
        //    //work自身也有Transform，但不能把他自己放到对象池中
        //    if (i != 0)
        //    {
        //        ObjectPool.Instance.BackObj(GameObject.Find("Pool/work").GetComponentsInChildren<Transform>()[i].gameObject);
        //    }            
        //}        
        EventCenter.Broadcast(CL.EventType.ShowUIGameOver);  
        DataManager.Instance.GameSettlement();
        DataManager.Instance.BestScoreRank();        
    }
    public void UIGameOverClose()
    {
        EventCenter.Broadcast(CL.EventType.HideUIGameOver);
    }

    public void UIShopOpen()
    {
        UIMainClose();
        EventCenter.Broadcast(CL.EventType.ShowUIShop);
        EventCenter.Broadcast(CL.EventType.UpdateDiamondCountUI);
    }
    public void UIShopClose()
    {
        EventCenter.Broadcast(CL.EventType.HideUIShop);
        EventCenter.Broadcast(CL.EventType.ShowUIMain);//没有UI框架导致的特殊情况，不能用UIMainOpen
    }

    public void UIResetUserDataOpen()
    {
        EventCenter.Broadcast(CL.EventType.ShowUIResetUserData);
    }   
    public void UIResetUserDataClose()
    {
        EventCenter.Broadcast(CL.EventType.HideUIResetUserData);
    }

    public void UIRankOpen()
    {
        EventCenter.Broadcast(CL.EventType.ShowUIRank);       
    }
    public void UIRankClose()
    {
        EventCenter.Broadcast(CL.EventType.HideUIRank);
    }

    #endregion



    #region 游戏状态（按钮点击以及特殊事件如玩家死亡等更改游戏状态）
    /// <summary>
    /// 游戏开始
    /// </summary>
    public void OnGameStart()
    {
        isGameStart = true;
        isGameControl = true;
        isPause = false;
        isGameOver = false;
        mainPlayer = GameObject.Find("Player");        
    }
    /// <summary>
    /// 游戏暂停
    /// </summary>
    public void OnGamePause()
    {
        Time.timeScale = 0;
        isGameControl = false;
        isPause = true;
    }
    /// <summary>
    /// 游戏继续
    /// </summary>
    public void OnGameContinue()
    {
        Time.timeScale = 1;
        isGameControl = true;
        isPause = false;
    }
    /// <summary>
    /// 游戏结束
    /// </summary>
    public void OnGameOver()
    {
        isGameStart = false;
        isGameControl = false;
        isGameOver = true;
    }
    #endregion
}
